<template>
  <div class="xinxi">
    <img class="top" src="./img2/screeningBgTop.png" alt="">
    <div class="bot">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="ruleForm.userName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="userCardId">
          <el-input v-model="ruleForm.userCardId"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="ruleForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="年龄" prop="age" >
          <el-input v-model="ruleForm.age" disabled></el-input>
        </el-form-item>
        <el-form-item label="教育程度" prop="education">
          <el-select v-model="ruleForm.education" placeholder="请选择教育程度">
            <el-option label="小学及以下" value="0"></el-option>
            <el-option label="初中" value="2"></el-option>
            <el-option label="高中" value="3"></el-option>
            <el-option label="大专" value="4"></el-option>
            <el-option label="本科" value="5"></el-option>
            <el-option label="硕士" value="6"></el-option>
            <el-option label="博士" value="7"></el-option>
          </el-select>
        </el-form-item>

      </el-form>
    </div>
    <div class="but">
      <el-row>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
      </el-row>
      <div class="text"> 您的数据会脱敏处理，我们将保护您的隐私安全 </div>
    </div>

  </div>
</template>
<script>
import axios from "axios";
import {
  getUrl
} from "@/config.js";
import { userInfo } from "os";
export default {
  props: {

  },
  data() {
    return {
      data: {},
      imgUrl: "",
      mechanismMemberUserId: "",
      phone:'',
      ruleForm: {
        userName: '',
        userCardId: '',
        age: '',
        education: '',
        delivery: false,
        type: [],
        sex: '',
        desc: '',
        
      },
      rules: {
        userName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        userCardId: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
          { validator: this.idcard, trigger: "blur" },

        ],
        birthday: [{ required: true, message: '请输入身份证自动获取出生日期', trigger: ['change'] }],
        education: [{ required: true, message: '请选择教育程度', trigger: 'blur'}],
      }

    };
  },
  computed: {

  },
  methods: {


    getData(){
      var baseUrl = getUrl();
      var phone = ''
      var token = localStorage.getItem('token')
      axios.get(
        baseUrl + "/an/wechat_app/wechat_user/info?token="+token,
        // `https://www.ainaoxingdong.com/api/an/wechat_app/wechat_user/report/bind_img?token=${_this.token}`,
        {
          // phone: this.phoneInfo,
          // isSuggest: 3
        }
      ).then((res) => {
        const userInfo = res.data.rows[0];
        console.log(userInfo);

        this.ruleForm.userName = userInfo.userName
        this.ruleForm.userCardId = userInfo.userCardId
        this.ruleForm.sex = userInfo.sex
        this.ruleForm.education = userInfo.education
        this.ruleForm.age = userInfo.age

        this.phone = userInfo.phone
        console.log(phone);
        if (res.data.code == 200) {

          // debugger
          // localStorage.setItem("token", res.data.data.token);
          // this.$router.push('/activity')
        } else {
          // this.$message.error('信息获取失败！');
        }
      });
    },
    async idcard(rule, value, callback) {
      if (!value) {
        if (rule.field == "id") {
          return callback(new Error("身份证号码不能为空"));
        } else {
          return;
        }
      } else {
        var num = value.charAt(16);
        console.log('数字', num);
        if (num % 2 == 0) {
          console.log(num)
          this.ruleForm.sex = 0
        } else {
          this.ruleForm.sex = 1
        }
        localStorage.setItem('sex',this.ruleForm.sex)
      }
      //setTimeout(() => {
      let regIdCard = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
      //如果通过该验证，说明身份证格式正确，但准确性还需计算
      if (regIdCard.test(value)) {
        await this.go(value.length);
        // if (value.length == 18) {
        //   var idCardWi = new Array(
        //     7,
        //     9,
        //     10,
        //     5,
        //     8,
        //     4,
        //     2,
        //     1,
        //     6,
        //     3,
        //     7,
        //     9,
        //     10,
        //     5,
        //     8,
        //     4,
        //     2
        //   ); //将前17位加权因子保存在数组里
        //   var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
        //   var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
        //   for (var i = 0; i < 17; i++) {
        //     idCardWiSum += value.substring(i, i + 1) * idCardWi[i];
        //   }
        //   var idCardMod = idCardWiSum % 11; //计算出校验码所在数组的位置
        //   var idCardLast = value.substring(17); //得到最后一位身份证号码
        //   //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
        //   if (idCardMod == 2) {
        //     if (idCardLast == "X" || idCardLast == "x") {
        //       callback();
        //     } else {
        //       callback(new Error("身份证号码错误"));
        //     }
        //   } else {
        //     //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
        //     if (idCardLast == idCardY[idCardMod]) {
        //       callback();
        //     } else {
        //       callback(new Error("身份证号码错误"));
        //     }
        //   }
        // }
      } else {
        callback(new Error("身份证格式不正确"));
      }
      //}, 1000);
    },
    go(val) {
      let iden = this.ruleForm.userCardId;
      let sex = null;
      let birth = null;
      let myDate = new Date();
      let month = myDate.getMonth() + 1;
      let day = myDate.getDate();
      let age = 0;
      if (val === 18) {
        age = myDate.getFullYear() - iden.substring(6, 10) - 1;
        sex = iden.substring(16, 17);
        birth = iden.substring(6, 10) + "-" + iden.substring(10, 12) + "-" + iden.substring(12, 14);
        if (iden.substring(10, 12) < month || iden.substring(10, 12) == month && iden.substring(12, 14) <= day) age++;

      }
      if (val === 15) {
        age = myDate.getFullYear() - iden.substring(6, 8) - 1901;
        sex = iden.substring(13, 14);
        birth = "19" + iden.substring(6, 8) + "-" + iden.substring(8, 10) + "-" + iden.substring(10, 12);
        if (iden.substring(8, 10) < month || iden.substring(8, 10) == month && iden.substring(10, 12) <= day) age++;
      }

      this.ruleForm.age = age;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(this.ruleForm));
          console.log(params, '我的')
          var baseUrl = getUrl();
          var token = localStorage.getItem("token");
          params.marriage = 0
					params.occupation = 0
          params.education = this.ruleForm.education
          params.phone = this.phone
          axios.post(
            baseUrl + "/an/wechat_app/wechat_user/submit?token=" + token,
            params
          ).then((res) => {
            const {
              data
            } = res
            // debugger
            this.data = data.data || {}
            console.log('data123', res.data.data.url)
            location.href = (res.data.data.url)
          });
          
        } else {
          this.$message.error('请检查填写信息！')
          return false;
        }
      });

    },


  },
  mounted() {
   this.getData()
     

  },
};
</script>

<style lang="scss">
.xinxi {
  width: 100%;
  height: 100%;
  background-color: #F9F9F9;
}

.top {
  width: 100%;

}

.bot {
  width: 100%;
  background-color: #ffffff;
  padding: 20px;
  margin-top: -50px;
  border-radius: 30px;
  position: relative;
  z-index: 111;
}

.but {
  width: 100%;
  margin-top: 50px;
  text-align: center;
}

.el-button--medium {
  width: 70%;
  height: 50px;
  border-radius: 10px;
  font-size: 20px;
}

.text {
  margin-top: 5px;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #336c74;
}

.tipText {
  height: 10px;
  color: gray;
  text-align: center;
  font-size: 20px;
  margin-bottom: 20px;
}
</style>
